package com.ys.tdm.dao.code.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.ys.tdm.bean.code.DataBase;
import com.ys.tdm.dao.code.IDataBaseDao;
import com.ys.tdm.templ.MybatisTemplate;
import com.ys.tdm.util.StringTools;
import com.ys.tdm.util.UUIDGenerator;




/**
 * 数据库链接信息Dao实现
 * @author hhj
 * @date 2018-03-07 10:13:57
 */
@Repository
public class DataBaseDaoImpl extends MybatisTemplate implements IDataBaseDao {

	@Override
	public DataBase getDataBaseById(String id) throws Exception {
		return (DataBase)this.selectOne("DataBaseXML.getDataBaseById", id);
	}
	
	@Override
	public List<DataBase> getDataBaseByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		return this.selectList("DataBaseXML.getDataBaseByIds", params);
	}
	
	@Override
	public List<DataBase> getDataBaseByIdsList(List<String> ids) throws Exception {
		return this.selectList("DataBaseXML.getDataBaseByIdsList", ids);
	}

	@Override
	public List<DataBase> getAll(DataBase dataBase) throws Exception {
		return this.selectList("DataBaseXML.getAll", dataBase);
	}

	/*@Override
	public PagerModel<DataBase> getPagerModelByQuery(DataBase dataBase, Query query)
			throws Exception {
		return this.getPagerModelByQuery(dataBase, query, "DataBaseXML.getPagerModelByQuery");
	}*/
	
	@Override
	public int getByPageCount(DataBase dataBase)throws Exception {
		return this.selectOne("DataBaseXML.getByPageCount", dataBase);
	}

	@Override
	public void insertDataBase(DataBase dataBase) throws Exception {
		if (null != dataBase) {
			dataBase.setId(UUIDGenerator.generate());
			dataBase.setCreateTime(new Date());
			dataBase.setUpdateTime(new Date());
			this.insert("DataBaseXML.insertDataBase", dataBase);
		}
	}
	
	@Override
	public void insertDataBaseBatch(List<DataBase> dataBases) throws Exception {
		if(CollectionUtils.isNotEmpty(dataBases)){
			for (DataBase dataBase : dataBases) {
				if (null != dataBase) {
					dataBase.setId(UUIDGenerator.generate());
					dataBase.setCreateTime(new Date());
				}
			}
			this.insert("DataBaseXML.insertDataBaseBatch", dataBases);
		}
	}
	
	@Override
	public void delDataBaseById(String id) throws Exception {
		this.delete("DataBaseXML.delDataBaseById", id);
	}
	
	@Override
	public void delDataBaseByIds(String ids) throws Exception {
		Map<String,Object> params = new HashMap<String, Object>();
		params.put("ids", ids);
		this.delete("DataBaseXML.delDataBaseByIds", params);
	}
	
	@Override
	public void delDataBaseByIdsList(List<String> ids) throws Exception {
		this.delete("DataBaseXML.delDataBaseByIdsList", ids);
	}
	
	@Override
	public int updateDataBase(DataBase dataBase) throws Exception {
		if (null != dataBase) {
			dataBase.setUpdateTime(new Date());
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(dataBase);
			return this.update("DataBaseXML.updateDataBase", dataBase);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateDataBaseByIds(String ids,DataBase dataBase) throws Exception {
		ids = StringTools.converString(ids);
		if (StringUtils.isNotBlank(ids) && null != dataBase) {
			dataBase.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(dataBase);
			params.put("dataBase", dataBase);
			return this.update("DataBaseXML.updateDataBaseByIds", params);
		} else {
			return 0;
		}
		
	}
	
	@Override
	public int updateDataBaseByIdsList(List<String> ids,DataBase dataBase) throws Exception {
		if (CollectionUtils.isNotEmpty(ids) && null != dataBase) {
			dataBase.setUpdateTime(new Date());
			Map<String,Object> params = new HashMap<String, Object>();
			params.put("ids", ids);
			/**清理不需要更新的数据*/
		    this.cleanWhenUpdate(dataBase);
			params.put("dataBase", dataBase);
			return this.update("DataBaseXML.updateDataBaseByIdsList", params);
		} else {
			return 0;
		}
	}
	
	@Override
	public int updateDataBaseList(List<DataBase> dataBases) throws Exception {
		if(CollectionUtils.isNotEmpty(dataBases)){
			for (DataBase dataBase : dataBases) {
				if (null != dataBase) {
					dataBase.setUpdateTime(new Date());
					/**清理不需要更新的数据*/
					this.cleanWhenUpdate(dataBase);
				}
			}
			return this.update("DataBaseXML.updateDataBaseList", dataBases);
		} else {
			return 0;
		}
	}
	
	/**
	 * 清理不需要更新的数据
	 * @param orderRebok
	 * @Description:
	 * @author wentaoxiang 2016年6月1日 下午5:19:16
	 */
	private void cleanWhenUpdate(DataBase dataBase) {
		dataBase.setCreateTime(null);
		dataBase.setCreator(null);
	}

	@Override
	public List<String> getAllDataBase() throws Exception {
		return this.selectList("DataBaseXML.getAllDataBase");
	}
	
	//------------api------------
}

